package com.viethp.mt.activities;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.mapoverlay.MyItemizedOverlay;

import android.app.ProgressDialog;
import com.viethp.mt.activities.R;

public class ANearbyGroupMap extends MapActivity implements OnClickListener,
		Runnable {

	private MapController mControl;
	private GeoPoint GeoP;
	private MapView mapV;
	private Button btn_list, btn_all, btn_want, btn_have;
	private ProgressDialog progressDialog;
	private Thread progressThread;

	public static Context context;
	private List<Overlay> mapOverlays;
	private Drawable wantdraw, havedraw;
	private MyItemizedOverlay wantitemizedOverlay, haveitemizedOverlay;
	private boolean wantIsDisplayed = false;
	private boolean haveIsDisplayed = false;

	// Define an array containing the food overlay items

	private OverlayItem[] WantItem = {
			new OverlayItem(new GeoPoint(16093815, 108214989), "Want Title 1",
					"Want snippet 1"),
			new OverlayItem(new GeoPoint(16083815, 108214989), "Want Title 2",
					"Want snippet 2"),
			new OverlayItem(new GeoPoint(16073362, 108185463), "Want Title 3",
					"Want snippet 3"),
			new OverlayItem(new GeoPoint(16073362, 108190784), "Want Title 4",
					"Want snippet 4"),
			new OverlayItem(new GeoPoint(16073815, 108214989), "Want Title 5",
					"Want snippet 5") };

	// Define an array containing the access overlay items

	private OverlayItem[] HaveItem = {
			new OverlayItem(new GeoPoint(16090681, 108202972), "Have Title 1",
					"Have snippet 1"),
			new OverlayItem(new GeoPoint(16073599, 108224505), "Have Title 2",
					"Have snippet 2"),
			new OverlayItem(new GeoPoint(16083599, 108224505), "Have Title 3",
					"Have snippet 3"),
			new OverlayItem(new GeoPoint(16063599, 108224505), "Have Title 4",
					"Have snippet 4"),
			new OverlayItem(new GeoPoint(16060825, 108244514), "Have Title 5",
					"Have snippet 5") };

	String TAG = "GPStest";
	// Set up the array of GeoPoints defining the route
	int numberRoutePoints;
	GeoPoint routePoints[]; // Dimension will be set in class RouteLoader below
	int routeGrade[]; // Index for slope of route from point i to point i+1
	boolean routeIsDisplayed = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		initComponents();
		setOnclickListener();
		setMap();
		initData();
	}

	public void initComponents() {
		// map init
		mapV = (MapView) findViewById(R.id.mapView);
		mapV.setTraffic(true);

		// button
		// btn_list = (Button) findViewById(R.id.btn_list);
		// btn_all = (Button) findViewById(R.id.btn_all);
		// btn_want = (Button) findViewById(R.id.btn_want);
		// btn_have = (Button) findViewById(R.id.btn_have);

		context = getApplicationContext();
	}

	public void setOnclickListener() {
		btn_list.setOnClickListener(this);
		btn_all.setOnClickListener(this);
		btn_want.setOnClickListener(this);
		btn_have.setOnClickListener(this);
	}

	public void setMap() {
		GeoP = new GeoPoint((int) (GlobalVariables.lat * 1E6),
				(int) (GlobalVariables.lon * 1E6));

		mControl = mapV.getController();
		mapV.setBuiltInZoomControls(true);
		mControl.animateTo(GeoP);
		mControl.setZoom(14);
		mapV.invalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void setWantItems() {
		int wantLength = WantItem.length;
		if (!wantIsDisplayed) {
			mapOverlays = mapV.getOverlays();
			wantdraw = this.getResources().getDrawable(R.drawable.pin_want);
			wantitemizedOverlay = new MyItemizedOverlay(wantdraw, mapV);
			for (int i = 0; i < wantLength; i++) {
				wantitemizedOverlay.addOverlay(WantItem[i]);
			}
			mapOverlays.add(wantitemizedOverlay);
			wantIsDisplayed = !wantIsDisplayed;
		}
		mapV.postInvalidate();
	}

	public void setHaveItems() {
		int haveLength = HaveItem.length;
		if (!haveIsDisplayed) {
			mapOverlays = mapV.getOverlays();
			havedraw = this.getResources().getDrawable(R.drawable.pin_have);
			haveitemizedOverlay = new MyItemizedOverlay(havedraw, mapV);
			for (int i = 0; i < haveLength; i++) {
				haveitemizedOverlay.addOverlay(HaveItem[i]);
			}
			mapOverlays.add(haveitemizedOverlay);
			haveIsDisplayed = !haveIsDisplayed;
		}
		mapV.postInvalidate();
	}

	public void removeWantItems() {
		int wantlength = WantItem.length;
		if (wantIsDisplayed) {
			for (int i = 0; i < wantlength; i++) {
				wantitemizedOverlay.removeItem(wantlength - 1 - i);
			}
			if ((wantitemizedOverlay.size() < 1))
				wantIsDisplayed = false;
			wantitemizedOverlay.hideOtherBalloons(mapOverlays);
		}
	}

	public void removeHaveItems() {
		int havelength = HaveItem.length;
		if (haveIsDisplayed) {
			for (int i = 0; i < havelength; i++) {
				haveitemizedOverlay.removeItem(havelength - 1 - i);
			}
			if ((haveitemizedOverlay.size() < 1))
				haveIsDisplayed = false;
			haveitemizedOverlay.hideOtherBalloons(mapOverlays);
		}
	}

	public int setAllItems() {
		if (!wantIsDisplayed)
			setWantItems();
		else if (!haveIsDisplayed)
			setHaveItems();
		return GlobalVariables.ReOK;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		// case R.id.btn_list:
		// View view = ATabNearByGroup.group
		// .getLocalActivityManager()
		// .startActivity(
		// "ANearbyGroupList",
		// new Intent(v.getContext(), ANearbyGroupList.class)
		// .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
		// .getDecorView();
		// ATabNearByGroup.group.replaceView(view);
		// break;
		// case R.id.btn_want:
		// //removeWantItems();
		// removeHaveItems();
		// setWantItems();
		// break;
		// case R.id.btn_have:
		// removeWantItems();
		// //removeHaveItems();
		// setHaveItems();
		// break;
		// case R.id.btn_all:
		// setAllItems();
		}

	}

	public void initData() {

		if (CommonHelper.isNetworkAvailable(this)) {
			startCallService();
		} else {
			CommonHelper.showWarning(this, getString(R.string.netstate));
		}

	}

	private void release() {
		if (progressThread != null && !progressThread.isInterrupted()) {
			progressThread.interrupt();
			progressThread = null;
		}
	}

	private void startCallService() {
		if (progressThread == null) {
			progressDialog = ProgressDialog.show(getParent(), "",
					"Please wait...", true);
			progressDialog.setCancelable(false);
			progressThread = new Thread(ANearbyGroupMap.this);
			progressThread.start();
		}
	}

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			progressDialog.dismiss();
			release();
			if (msg.what == GlobalVariables.ReOK) {
				mapV.postInvalidate();
			} else {
				CommonHelper.showWarning(ANearbyGroupMap.this,
						getString(R.string.loaddatastate));

			}

		};
	};

	public void run() {
		Message message = new Message();
		message.what = setAllItems();
		handler.sendMessage(message);

	}

	// private int callService(){
	// if(WantItem != null){
	// for(OverlayItem ovl : WantItem){
	// mtrade mt = new mtrade();
	//
	// mt.setTitle(ovl.getTitle());
	// mt.setDesc(ovl.getSnippet());
	// mt.setLat(ovl.getPoint().getLatitudeE6());
	// mt.setLong(ovl.getPoint().getLongitudeE6());
	// mt.setIsWant(true);
	//
	// listmtrade.add(mt);
	// }
	// }
	// if(HaveItem != null){
	// for(OverlayItem ovl : HaveItem){
	// mtrade mt = new mtrade();
	//
	// mt.setTitle(ovl.getTitle());
	// mt.setDesc(ovl.getSnippet());
	// mt.setLat(ovl.getPoint().getLatitudeE6());
	// mt.setLong(ovl.getPoint().getLongitudeE6());
	// mt.setIsWant(false);
	//
	// listmtrade.add(mt);
	// }
	// }
	// return GlobalVariables.ReOK;
	// }

}
